package org.lmind.example.common.service.auth;

import java.util.ArrayList;

import org.lmind.example.common.api.auth.AuthenticationException;
import org.lmind.example.common.api.auth.AuthenticationProvider;
import org.lmind.example.common.api.auth.Certificate;
import org.lmind.example.common.api.auth.AuthenticationService;
import org.lmind.example.common.api.auth.Credential;

public class GeneralAuthenticationService implements AuthenticationService {
	
	private ArrayList<AuthenticationProvider> providers;
	
	public void setProviders(ArrayList<AuthenticationProvider> providers) {
		this.providers = providers;
	}


	@Override
	public Certificate authenticate(Credential credential) {
		
		Class<?> clazz = credential.getClass();
		for(AuthenticationProvider provider : providers) {
			if (!provider.supports(clazz)) {
				continue;
			}
			return provider.authenticate(credential);
		}
		throw new AuthenticationException("不支持类型为" + clazz.getName() + "的认证");
	}
	
	
	/**
	 * 已颁发的证书
	 * @author mian.liu
	 *
	 */
	private class AwardedCertificate {
		public Credential credential;
		public Certificate certificate;
	}

}
